package com.ssmTest.controller;

import com.ssmTest.domian.User;
import com.ssmTest.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private HttpServletRequest request;


    @RequestMapping("/login")
    public ModelAndView login(User user,ModelAndView mv){
        User loginUser = userService.login(user);
        if (loginUser!=null){
            request.getSession().setAttribute("loginUser",loginUser);
            mv.setViewName("redirect:/stu/findAll.do");
        }else {
            mv.addObject("msg","您输入的用户名或密码有误");
            mv.setViewName("forward:/login.jsp");
        }

        return mv;
    }


   /* @RequestMapping("/login")
    public String login(User user){
        User loginUser = userService.login(user);
        HttpSession session = request.getSession();
        if (loginUser!=null){
            session.setAttribute("loginUser",loginUser);
            return "redirect:/stu/findAll.do";
        }
            request.setAttribute("msg","您输入的用户名或密码有误");
        return "login";
    }*/

}
